﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmReloj
{
    public partial class AbmReloj : Form
    {
        private Modelo.FormActions.Actions action = Modelo.FormActions.Actions.Alta;
        private int selected_elem = -1;

        public AbmReloj(Modelo.FormActions.Actions runAction, int id)
        {
            InitializeComponent();

            SqlConnection con2 = Modelo.Modelo.createConnection();
            SqlDataAdapter da2 = new SqlDataAdapter(Modelo.SqlQueries.getMarcasReloj(), con2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            reloj_cb.DataSource = ds2.Tables[0].DefaultView;
            reloj_cb.DisplayMember = "Marca";
            reloj_cb.ValueMember = "Id";
            reloj_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con2);

            switch (runAction)
            {
                case Modelo.FormActions.Actions.Baja:
                    selected_elem = id;
                    this.loadData(selected_elem);
                    action = Modelo.FormActions.Actions.Baja;
                    reloj_cb.Enabled = false;
                    tb_modelo.Enabled = false;
                    tb_nroserie.Enabled = false;
                    verific_dtp.Enabled = false;
                    cb_hab.Enabled = false;
                    aceptar.Text = "Eliminar";
                    break;
                case Modelo.FormActions.Actions.Modificacion:
                    selected_elem = id;
                    this.loadData(selected_elem);
                    action = Modelo.FormActions.Actions.Modificacion;
                    aceptar.Text = "Modificar";
                    break;
            }

        }

        public AbmReloj(Modelo.FormActions.Actions runAction)
        {
            InitializeComponent();

            SqlConnection con2 = Modelo.Modelo.createConnection();
            SqlDataAdapter da2 = new SqlDataAdapter(Modelo.SqlQueries.getMarcasReloj(), con2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            reloj_cb.DataSource = ds2.Tables[0].DefaultView;
            reloj_cb.DisplayMember = "Marca";
            reloj_cb.ValueMember = "Id";
            reloj_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con2);

            cb_hab.Enabled = false;
        }

        private void loadData(int id)
        {
            using (SqlConnection con3 = Modelo.Modelo.createConnection())
            {
                Modelo.Modelo.openConnection(con3);
                SqlCommand comando_get_autos = new SqlCommand(Modelo.SqlQueries.getRelojById(selected_elem), con3);
                SqlDataReader reader = comando_get_autos.ExecuteReader();
                while (reader.Read())
                {
                    reloj_cb.SelectedValue = Convert.ToInt32(reader["Marca"]);
                    tb_modelo.Text = reader["Modelo"].ToString();
                    tb_nroserie.Text = reader["NroSerie"].ToString();
                    verific_dtp.Value = Convert.ToDateTime(reader["FecVerific"]);
                    cb_hab.Checked = (Boolean)reader["Habilitado"];
                }
            }
        }

        private void aceptar_Click(object sender, EventArgs e)
        {
            switch (action)
            {
                case Modelo.FormActions.Actions.Alta:
                    using (SqlConnection con = Modelo.Modelo.createConnection())
                    {
                        try
                        {
                            Modelo.Modelo.openConnection(con);
                            SqlCommand comando_add_reloj = new SqlCommand(Modelo.SqlQueries.addReloj(
                                                        Convert.ToInt32(reloj_cb.SelectedValue),
                                                        tb_modelo.Text,
                                                        tb_nroserie.Text,
                                                        verific_dtp.Value,
                                                        true), con);
                            comando_add_reloj.ExecuteNonQuery();
                            MessageBox.Show("Reloj agregado");
                            Login.Login.mainForm.openForm(new AbmReloj(Modelo.FormActions.Actions.Alta));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
                case Modelo.FormActions.Actions.Baja:
                    using (SqlConnection con_un = Modelo.Modelo.createConnection())
                    {
                        try
                        {
                            Modelo.Modelo.openConnection(con_un);
                            SqlCommand comando_check_unique = new SqlCommand(Modelo.SqlQueries.getRelojUsado(selected_elem), con_un);
                            SqlDataReader reader = comando_check_unique.ExecuteReader();
                            if (reader.Read())
                            {
                                MessageBox.Show("Reloj estaba siendo usado por un taxi habilitado");
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    using (SqlConnection con = Modelo.Modelo.createConnection())
                    {
                        try
                        {
                            Modelo.Modelo.openConnection(con);
                            SqlCommand comando = new SqlCommand(Modelo.SqlQueries.deleteReloj(selected_elem), con);
                            comando.ExecuteNonQuery();
                            MessageBox.Show("Reloj eliminado");
                            Login.Login.mainForm.openForm(new AbmReloj(Modelo.FormActions.Actions.Alta));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
                case Modelo.FormActions.Actions.Modificacion:
                    using (SqlConnection connection = Modelo.Modelo.createConnection())
                    {
                        try
                        {
                            Modelo.Modelo.openConnection(connection);
                            SqlCommand comando = new SqlCommand(Modelo.SqlQueries.updateReloj(selected_elem,
                                                        Convert.ToInt32(reloj_cb.SelectedValue),
                                                        tb_modelo.Text,
                                                        tb_nroserie.Text,
                                                        verific_dtp.Value,
                                                        cb_hab.Checked), connection);
                            comando.ExecuteNonQuery();
                            MessageBox.Show("Reloj modificado");
                            Login.Login.mainForm.openForm(new AbmReloj(Modelo.FormActions.Actions.Alta));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
            }
        }
    }
}
